<template>
  <a-card :bordered="false">

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button v-if="!disabledButton" @click="handleAdd" type="primary" icon="plus" :disabled="isDisabledAuth('erpEmployeeTraining:add')">新增</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: '100%' }"
        :components="draggableComponent"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="false"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange">
        <template slot="status" slot-scope="status">
          <a-badge :status="status | statusTypeFilter" :text="status | statusFilter"/>
        </template>
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button v-else icon="download" @click="viewFile(text)">文件列表</a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <template v-if="!disabledButton">
            <a-button
              class="ruleButton"
              :ghost="true"
              type="primary"
              icon="edit"
              @click="handleEdit(record)"
              :disabled="isDisabledAuth('erpEmployeeTraining:edit')"
            />
            <a-divider type="vertical"/>
            <a-popconfirm title="确定删除吗?" @confirm="() => listDelete(record.id)">
              <a-button
                class="ruleButton"
                :ghost="true"
                type="primary"
                icon="delete"
                :disabled="isDisabledAuth('erpEmployeeTraining:delete')"/>
            </a-popconfirm>
          </template>
        </span>

      </a-table>
    </div>

    <erp-employee-training-modal ref="modalForm" @ok="handleOk"></erp-employee-training-modal>
    <view-file-modal ref="viewFileModal"></view-file-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import ErpEmployeeTrainingModal from './modules/ErpEmployeeTrainingModal'
  import { DisabledAuthFilterMixin } from '@/mixins/DisabledAuthFilterMixin'
  import { viewFile } from '@/utils/util'
  import ViewFileModal from '@/views/scy/components/ViewFileModal'
  import { filterMultiDictText, initDictOptions } from '@/components/dict/JDictSelectUtil'
  export default {
    name: 'ErpEmployeeTrainingList',
    mixins: [JeecgListMixin, mixinDevice, DisabledAuthFilterMixin, viewFile],
    components: {
      ErpEmployeeTrainingModal,
      ViewFileModal
    },
    props: {
      disabledButton: {
        type: Boolean,
        default: false
      },
      newData: {
        type: Array,
        default: () => []
      }
    },
    data () {
      return {
        description: '员工档案-培训经历管理页面',
        // 表头
        columns: [],
        // 列设置
        settingColumns: [],
        // 列定义
        defColumns: [
          {
            title: '序号',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: 'center',
            customRender: function (t, r, index) {
              return parseInt(index) + 1;
            }
          },
          {
            title: '培训开始日期',
            align: 'center',
            width: 110,
            ellipsis: true,
            dataIndex: 'beginDate',
            customRender: function (text) {
              return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
            }
          },
          {
            title: '培训结束日期',
            align: 'center',
            width: 110,
            ellipsis: true,
            dataIndex: 'endDate',
            customRender: function (text) {
              return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
            }
          },
          {
            title: '培训机构',
            align: 'center',
            width: 160,
            ellipsis: true,
            dataIndex: 'institution'
          },
          {
            title: '培训项目',
            align: 'center',
            width: 160,
            ellipsis: true,
            dataIndex: 'project'
          },
          {
            title: '取得证书名称',
            align: 'center',
            width: 160,
            ellipsis: true,
            dataIndex: 'certificate'
          },
          {
            title: '附件',
            align: 'center',
            width: 130,
            ellipsis: true,
            dataIndex: 'attachment',
            scopedSlots: { customRender: 'fileSlot' }
          },
          {
            title: '培训服务期限',
            align: 'center',
            width: 105,
            ellipsis: true,
            dataIndex: 'trainingServiceLimit',
            customRender: (text) => {
              if (!text) {
                return ''
              } else {
                return filterMultiDictText(this.dictOptions['trainingServiceLimit'], text + '')
              }
            },
          },
          {
            title: '服务期限起止时间',
            align: 'center',
            dataIndex: 'serviceStartDate',
            width: 130,
            ellipsis: true,
            customRender: function (text, record) {
              console.log('record', record)
              return record.serviceStartDate + '~' + record.serviceEndDate
            }
          },
          {
            title: '培训费用(元)',
            align: 'center',
            width: 100,
            ellipsis: true,
            dataIndex: 'trainingFee'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: 'center',
            fixed: 'right',
            width: 147,
            scopedSlots: {
            filterDropdown: 'filterDropdown',
            filterIcon: 'filterIcon',
            customRender: 'action'
            }
          }
        ],
        url: {
          list: '/api/erp/files/employee/erpEmployeeTraining/list',
          list0: '/api/erp/files/employee/erpEmployeeTraining/list?employeeId=',
          delete: '/api/erp/files/employee/erpEmployeeTraining/delete',
          deleteBatch: '/api/erp/files/employee/erpEmployeeTraining/deleteBatch',
          exportXlsUrl: '/api/erp/files/employee/erpEmployeeTraining/exportXls',
          exportXlsUrl0: '/api/erp/files/employee/erpEmployeeTraining/exportXls?employeeId=',
          importExcelUrl: 'api/erp/files/employee/erpEmployeeTraining/importExcel',
          importExcelUrl0: 'api/erp/files/employee/erpEmployeeTraining/importExcel?foreignKey=employeeId&mainId='
        },
        dictOptions: {},
        superFieldList: [],
        disableMixinCreated: true,
        colsettings: 'ErpEmployeeTraining:colsettings'
      }
    },
    watch: {
      $route(to, from) {
        this.url.exportXlsUrl = this.url.exportXlsUrl0 + this.$route.params.id;
        this.url.importExcelUrl = this.url.importExcelUrl0 + this.$route.params.id;
      },
      newData(newValue) {
        this.dataSource = Object.assign([], newValue)
      }
    },
    created() {
      this.initDictConfig()
      this.getSuperFieldList();
      this.url.exportXlsUrl = this.url.exportXlsUrl0 + this.$route.params.id;
      this.url.importExcelUrl = this.url.importExcelUrl0 + this.$route.params.id;
      this.initColumns();
    },
    computed: {
      importExcelUrl: function() {
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      }
    },
    methods: {
      listDelete(id) {
        this.dataSource.forEach((val, index) => {
          if (val.id === id) {
            this.dataSource.splice(index, 1)
          }
        })
        this.$emit('delete', this.dataSource)
      },
      handleOk(val) {
        if (val.id) {
          let index = this.dataSource.findIndex(item => item.id === val.id)
          if (index > -1) {
            this.dataSource.splice(index, 1, val)
          }
        } else {
          this.dataSource.push({ id: parseInt((Math.random() * 1000000000)).toString(), ...val })
        }
        this.$emit('ok', this.dataSource)
      },
      initDictConfig() {
        initDictOptions('training_service_limit').then((res) => {
          if (res.success) {
            this.$set(this.dictOptions, 'trainingServiceLimit', res.result)
          }
        })
      },
      handleAdd: function() {
        this.$refs.modalForm.edit({
            'employeeId': this.$route.params.id
        })
        this.$refs.modalForm.title = '新增'
        this.$refs.modalForm.disableSubmit = false
      },
      getSuperFieldList() {
        let fieldList = [];
        fieldList.push({ type: 'date', value: 'beginDate', text: '开始时间' })
        fieldList.push({ type: 'date', value: 'endDate', text: '结束时间' })
        fieldList.push({ type: 'string', value: 'institution', text: '培训机构', dictCode: '' })
        fieldList.push({ type: 'string', value: 'project', text: '项目', dictCode: '' })
        fieldList.push({ type: 'string', value: 'certificate', text: '证书', dictCode: '' })
        fieldList.push({ type: 'Text', value: 'attachment', text: '附件', dictCode: '' })
        fieldList.push({ type: 'string', value: 'employeeId', text: '员工id', dictCode: '' })
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
